package com.application.metroscanner.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;

import android.content.Context;

import com.example.metroscanner.R;

public class FileManager {

	private FileManager() {
	}

	private static FileManager INSTANCE = new FileManager();
	private static Context context;

	public static FileManager getInstance(Context iContext) {
		context = iContext;
		return INSTANCE;
	}

	public void AddCSVLine(String iLine) {
		try {
			FileWriter writer = new FileWriter(new File(context.getFilesDir(),
					context.getString(R.string.CSV_rides)), true);
			writer.write(iLine + System.getProperty("line.separator"));
			writer.close();
		} catch (Exception e) {
		}
	}

	public ArrayList<String> ReadFile(String iFileName) {
		ArrayList<String> res = new ArrayList<String>();
		FileInputStream inputStream;
		BufferedReader reader;
		// try to read
		try {
			inputStream = context.openFileInput(context
					.getString(R.string.CSV_rides));
			reader = new BufferedReader(new InputStreamReader(inputStream));

			// for each line
			String line = "";
			while ((line = reader.readLine()) != null) {
				res.add(line);
			}
			// close file
			inputStream.close();
		} catch (Exception e) {
		}

		return res;
	}

	public void GenerateTestFile() {
		try {
			File file = new File(context.getFilesDir(),	context.getString(R.string.CSV_rides));
			FileWriter writer = new FileWriter(file, false);
			
			Random rand = new Random();

			// format : dayOfWeek;month;day;hour;minute;time;rating
			// morning
			for (int i = 0; i < 100; i++) {
				int randomDayOfWeek = rand.nextInt(6) + 1;
				int randomMonth = rand.nextInt(11);
				int randomDay = rand.nextInt(30);				
				int randomMinute = rand.nextInt(30) + 30;
				
				double zeroAUn = (randomMinute - 30)/30.0;
				double variation = Math.sin(((zeroAUn * 2) + 1) * Math.PI) * 15 * 60 * 1000;
				
				int randomRideTime = (int)Math.floor(variation+0.5) + 3600000;
				
				int randomRating = rand.nextInt(5)+1;

				String line = randomDayOfWeek + ";" + randomMonth + ";"
						+ randomDay + ";9;" + randomMinute + ";"
						+ randomRideTime + ";" + randomRating;
				
				writer.write(line + System.getProperty("line.separator"));
			}

			for (int i = 0; i < 100; i++) {
				int randomDayOfWeek = rand.nextInt(6) + 1;
				int randomMonth = rand.nextInt(11);
				int randomDay = rand.nextInt(30);
				int randomMinute = rand.nextInt(30) + 30;
				
				double zeroAUn = (randomMinute - 30)/30.0;
				double variation = Math.sin(zeroAUn * 2 * Math.PI) * 15 * 60 * 1000;
				
				int randomRideTime = (int)Math.floor(variation+0.5) + 3600000;
				int randomRating = rand.nextInt(5)+1;

				String line = randomDayOfWeek + ";" + randomMonth + ";"
						+ randomDay + ";18;" + randomMinute + ";"
						+ randomRideTime + ";" + randomRating;
				
				writer.write(line + System.getProperty("line.separator"));
			}
			
			writer.close();
		} catch (Exception e) {
		}
	}
}
